﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTLiteLogic.Delegates;
using FTLiteLogic.Interface;
using FullThrustLogic.Helper;

namespace FullThrustLogic.ShipComponent.Weapon
{
    public class Torpedo : iWeapon, IReportableEvent
    {
        private double _range = 30;

        private ReportableEvent _eventReporter;
        public ReportableEvent EventReporter
        {
            get { return _eventReporter; }
        }

        public Torpedo()
        {
            _eventReporter = new ReportableEvent();
        }

        #region iWeapon Members

        public double Range
        {
            get
            {
                return _range;
            }
            set
            {
            }
        }

        public bool Enabled
        {
            get; set;
        }

        public Arc FireArc
        {
            get; set;
        }

        public void ExcuteAttack(Ship targetedShip, double distance)
        {
            if(Enabled)
            {
                int targetToHit = 6;

                if (distance <= 6)
                {
                    targetToHit = 2;
                }
                else if (distance <= 12)
                {
                    targetToHit = 3;
                }
                else if (distance <= 18)
                {
                    targetToHit = 4;
                }
                else if (distance <= 24)
                {
                    targetToHit = 5;
                }
                else if (distance <= 30)
                {
                    targetToHit = 6;
                }

                if (DiceHelper.DetermineSuccess(targetToHit, 1))
                {
                    StringBuilder eventDescription = new StringBuilder();
                    bool isEventSuccess = true;

                    int damage = DiceHelper.RollDice();
                    eventDescription.AppendLine(@"Hit player " + targetedShip.OwnerPlayer.PlayerType.ToString() + " ship " + targetedShip.Name + " for "+damage+" damage ");
                    _eventReporter.Report(this, new ReportableEventArgs() { EventDescription = eventDescription.ToString(), IsEventSuccess = isEventSuccess });
                    targetedShip.ApplyDamage(damage);
                }
            }
        }

        public bool InArc(Ship targetingShip, Ship targetedShip)
        {
            double onePointX = targetingShip.PositionX + 1 * Math.Cos(targetingShip.Heading);
            double onePointY = targetingShip.PositionY + 1 * Math.Sin(targetingShip.Heading);

            Double angle = Math.Atan2(targetedShip.PositionY - targetingShip.PositionY,
                targetedShip.PositionX - targetingShip.PositionX) - 
                Math.Atan2(targetingShip.PositionY - onePointY, targetingShip.PositionX - onePointX);


            if((angle >= 330 && angle <= 360) || angle <=30)
            {
                if(FireArc.Front)
                {
                    return true;
                }
            }
            else if (angle > 30 || angle <= 90)
            {
                if(FireArc.FrontStarboard)
                {
                    return true;
                }
            }
            else if (angle > 90 || angle <= 150)
            {
                if (FireArc.AftStarboard)
                {
                    return true;
                }
            }
            else if (angle > 150 || angle <= 210)
            {
                if (FireArc.Aft)
                {
                    return true;
                }
            }
            else if (angle > 210 || angle <= 270)
            {
                if (FireArc.AftPortside)
                {
                    return true;
                }
            }
            else if (angle > 270 || angle <= 330)
            {
                if (FireArc.AftPortside)
                {
                    return true;
                }
            }

            return false;
        }

       

        #endregion
    }
}
